package com.example.mtblog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.mtblog.dto.LoginUser;
import com.example.mtblog.entity.User;
import com.example.mtblog.mapper.UserMapper;
import jakarta.annotation.Resource;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
public class UserDetailServiceImpl implements UserDetailsService {

    @Resource
    UserMapper userMapper;

    /**
     * Description:验证用户信息
     * date: 2023/6/8
     * author: MT
     */
    @Override
    public UserDetails loadUserByUsername(String uid) throws UsernameNotFoundException {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUid,uid);
        User user = userMapper.selectOne(wrapper);

        if(Objects.isNull(user)){
            throw new UsernameNotFoundException("用户或密码错误");
        }
        return new LoginUser(user);
    }
}
